package com.sport.service;

import com.sport.constant.MessageConstant;
import com.sport.context.BaseContext;
import com.sport.exception.BaseException;
import com.sport.mapper.UserMapper;
import com.sport.pojo.dto.UpdateUserDto;
import com.sport.pojo.dto.UserShow;
import com.sport.pojo.entity.User;
import com.sport.pojo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserAllInfoVo login(String userAccount, String userPassword) {
        User user = null;
        user = userMapper.findUserByUserName(userAccount);
        if(user==null){
           user = userMapper.findUserByUserAccount(userAccount);
        }
        if (user==null){
            throw new BaseException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        userPassword = DigestUtils.md5DigestAsHex(userPassword.getBytes());
        if(!userPassword.equals(user.getUserPassword())){
            throw new BaseException(MessageConstant.PASSWORD_ERROR);
        }
        UserAllInfoVo userAllInfoVo = userMapper.findUserAllByUserId(user.getUserId());
        if(userAllInfoVo==null){
            UserAllInfoVo userAllInfoVo1 = new UserAllInfoVo();
            BeanUtils.copyProperties(user,userAllInfoVo1);
            return userAllInfoVo1;
        }
        if(userAllInfoVo.getImgId()!=null&&!userAllInfoVo.getUserId().equals(userAllInfoVo.getRoomhostId())){
            userAllInfoVo.setImgId(null);
        }
        if (userAllInfoVo.getCamp()==2){
            userAllInfoVo.setJudge("裁判");
        }
        return userAllInfoVo;
    }

    @Override
    public int register(User user) {
        String id = UUID.randomUUID().toString();
        user.setUserId(id);
        user.setUserName("用户"+id.substring(0,5));
        user.setAvatar("https://chen-y-h.oss-cn-hangzhou.aliyuncs.com/5b8cf31b-609c-45d8-84d8-77fb6977ab61.jpeg");
        user.setUserPassword(DigestUtils.md5DigestAsHex(user.getUserPassword().getBytes()));
        return userMapper.insertUser(user);
    }

    @Override
    public User findUserMessage(String userId) {
        return userMapper.findUserMessageByUserId(userId);
    }

    @Override
    public int userUpdateMessage(UpdateUserDto updateUserDto) {
        updateUserDto.setUserId(BaseContext.getCurrentId());
        return userMapper.userUpdateMessage(updateUserDto);
    }
    public List<HistoryVo> showHistory(String userId){
        List<HistoryVo> historyByUserId = userMapper.findHistoryByUserId(userId);
        if(historyByUserId==null){
            throw new BaseException("暂无历史战绩");
        }
        for (HistoryVo historyVo : historyByUserId) {
            if(historyVo.getCamp()==2){
                //裁判
                historyVo.setOutcome(3);
            }else if(historyVo.getOutcome()==2){
                //平局
                historyVo.setOutcome(2);
            }else if(historyVo.getOutcome()==historyVo.getCamp()){
                //胜利
                historyVo.setOutcome(1);
            } else {
                //失败
                historyVo.setOutcome(0);
            }
        }
        return historyByUserId;
    }

    @Override
    public int updatePassword(String password) {
        return userMapper.updatePassword(BaseContext.getCurrentId(),DigestUtils.md5DigestAsHex(password.getBytes()));
    }

    @Override
    public int updateAvatar(String path) {
        return userMapper.updateAvatar(BaseContext.getCurrentId(),path);
    }

    @Override
    public int studentVerification(String path) {
        return userMapper.studentVerification(BaseContext.getCurrentId(),path);
    }

    @Override
    public List<StudentVerifyVo> findStudentVerification() {
        List<StudentVerifyVo> userList = userMapper.findStudentVerification();
        if(userList==null){
            throw new BaseException("暂无数据");
        }
        return userMapper.findStudentVerification();
    }

    @Override
    public List<JudgeVerifyVo> findJudgeVerification() {
        List<JudgeVerifyVo> list = userMapper.findJudgeVerification();
        if(list==null){
            throw new BaseException("暂无数据");
        }
        return list;
    }

    @Override
    public int agreeStudentVerification(String userId) {
        return userMapper.agreeStudentVerification(userId);
    }

    @Override
    public int agreeJudgeVerification(String userId) {
        return  userMapper.agreeJudgeVerification(userId);
    }

    @Override
    public VerifyVo findVerify() {
       VerifyVo verifyVo = userMapper.findVerify(BaseContext.getCurrentId());
        return verifyVo;
    }

    @Override
    public int judgeVerification(String path) {
        return userMapper.judgeVerification(BaseContext.getCurrentId(),path);
    }

    @Override
    public int disagreeStudentVerification(String userId) {
        return userMapper.disagreeStudentVerification(userId);
    }

    @Override
    public int disagreeJudgeVerification(String userId) {
        return  userMapper.disagreeJudgeVerification(userId);
    }

    @Override
    public int getDao() {
        return userMapper.getDao(BaseContext.getCurrentId());
    }

    @Override
    public List<UserShow> showUserFootballDanRank() {
        return userMapper.showUserFootballDanRank();
    }
}
